package com.rongcheng.frc.newstyledemo.ui.player;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.rongcheng.frc.newstyledemo.R;
import com.rongcheng.frc.newstyledemo.utils.Util;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

public class PlayLoadLayout extends FrameLayout implements OnClickListener {

    private int errState = 0;

    private View loading;

    /**
     * 没有视频
     */
    private View not_play;

    /**
     * 请求错误提示
     */
    private View request_error;
    private TextView request_error_text;

    /**
     * 请求错误提示点击
     */
    private View request_error_btn;


    /**
     * 视频加载提示文字
     */
    private TextView loadingTxt;

    private PlayLoadLayoutCallBack callBack;

    public PlayLoadLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public PlayLoadLayout(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        inflate(context, R.layout.play_loading_layout, this);
        findView();
    }

    private void findView() {
        loading = findViewById(R.id.loading);
        not_play = findViewById(R.id.no_play_error);
        request_error = findViewById(R.id.request_error);
        request_error_text = (TextView) findViewById(R.id.request_error_text);
        request_error_btn = findViewById(R.id.request_error_btn);
        loadingTxt = (TextView) findViewById(R.id.loadingTxt);
        request_error_btn.setOnClickListener(this);
        resizeLoadingText(false);// 初始化竖屏
    }


    public void loading() {
        loading(false);
    }

    /**
     * 视频加载progress---数据加载中...
     *
     * @param showText 是否显示文字
     */
    public void loading(boolean showText) {
        loading.setVisibility(VISIBLE);
        if (showText) {
            loadingTxt.setText(R.string.player_loading);
            loadingTxt.setVisibility(VISIBLE);
        } else {
            loadingTxt.setVisibility(GONE);
        }
        not_play.setVisibility(GONE);
        request_error.setVisibility(GONE);
        if (loadingCallBack != null) {
            for (int i = 0; i < loadingCallBack.size(); i++) {
                if (loadingCallBack.get(i).get() != null) {
                    loadingCallBack.get(i).get().loading(true);
                } else {
                    loadingCallBack.remove(i);
                }
            }
        }
    }

    /**
     * 当正在加载的时候调用该回调，为了防止内存溢出，使用弱引用，但并不是很好的解决办法，最好后续在弄清逻辑之后，更改该部分代码
     */
    private ArrayList<WeakReference<LoadingCallBack>> loadingCallBack = new ArrayList<WeakReference<LoadingCallBack>>();

    public interface LoadingCallBack {
        void loading(boolean show);
    }

    public void setLoadingCallBack(LoadingCallBack loadingCallBack) {
        this.loadingCallBack.add(new WeakReference<LoadingCallBack>(loadingCallBack));
    }

    /**
     * 视频加载progress---即将播放...
     *
     * @param videoName
     */
    public void loadingVideo(String videoName) {
        loading.setVisibility(VISIBLE);
        loadingTxt.setVisibility(VISIBLE);
        String willPlay = getContext().getString(R.string.will_play);
        loadingTxt.setText(willPlay);
        not_play.setVisibility(GONE);
        request_error.setVisibility(GONE);
    }

    public void loadingStart(String loadingMsg, String videoName) {
        loading.setVisibility(VISIBLE);
        loadingTxt.setVisibility(VISIBLE);
        if (!TextUtils.isEmpty(loadingMsg)) {
            loadingTxt.setText(loadingMsg);
        } else if (!TextUtils.isEmpty(videoName)) {
            String willPlay = getContext().getString(R.string.will_play);
            loadingTxt.setText(willPlay);
        } else {
            loadingTxt.setText(getContext().getString(R.string.player_loading));
        }
        not_play.setVisibility(GONE);
        request_error.setVisibility(GONE);
    }

    /**
     * 重新layout loadingTxt在横屏和竖屏下的宽度=当前屏幕的宽度*0.6
     *
     * @param isLandscape
     */
    public void resizeLoadingText(boolean isLandscape) {
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) loadingTxt.getLayoutParams();
        params.width = (int) (Util.getScreenWidth() * 0.6f);
        loadingTxt.requestLayout();
    }

    /**
     * 是否有异常提示
     *
     * @return
     */
    public boolean isErrorTagShow() {
        return request_error.getVisibility() == View.VISIBLE || not_play.getVisibility() == View.VISIBLE;
    }

    public void finish() {
        if (callBack != null) {
            callBack.calledInFinish();
        }
        errState = 0;
        loading.setVisibility(View.GONE);
        loadingTxt.setVisibility(View.GONE);
        not_play.setVisibility(GONE);
        request_error.setVisibility(GONE);
        if (loadingCallBack.size() > 0) {
            for (int i = 0; i < loadingCallBack.size(); i++) {
                if (loadingCallBack.get(i).get() != null) {
                    loadingCallBack.get(i).get().loading(false);
                } else {
                    loadingCallBack.remove(i);
                }
            }
        }
    }

    public boolean isLoadingShow() {
        return loading.getVisibility() == View.VISIBLE || request_error.getVisibility() == View.VISIBLE;
    }

    public void notPlay() {
        errState = 1;
        loading.setVisibility(View.GONE);
        loadingTxt.setVisibility(View.GONE);
        not_play.setVisibility(VISIBLE);
        request_error.setVisibility(GONE);
    }

    public void requestError() {
        if (callBack != null) {
            callBack.calledInError();
        }
        errState = 2;
        loading.setVisibility(View.GONE);
        loadingTxt.setVisibility(View.GONE);
        not_play.setVisibility(GONE);
        request_error.setVisibility(VISIBLE);
    }

    public void requestError(String msg) {
        if (callBack != null) {
            callBack.calledInError();
        }
        errState = 2;
        loading.setVisibility(View.GONE);
        loadingTxt.setVisibility(View.GONE);
        not_play.setVisibility(GONE);
        request_error.setVisibility(VISIBLE);
        request_error_text.setText(msg);

    }

    public void cannotPlayError() {
        if (callBack != null) {
            callBack.calledInError();
        }
        errState = 2;
        loading.setVisibility(View.GONE);
        loadingTxt.setVisibility(View.GONE);
        not_play.setVisibility(View.VISIBLE);
        request_error.setVisibility(GONE);
    }

    @Override
    public void onClick(View v) {
        if (request_error_btn == v) {
            if (callBack != null) {
                callBack.onRequestErr();
            }
            errState = 0;
        }
    }

    public PlayLoadLayoutCallBack getCallBack() {
        return callBack;
    }

    public void setCallBack(PlayLoadLayoutCallBack callBack) {
        this.callBack = callBack;
    }

    /**
     * 点击回调
     */
    public interface PlayLoadLayoutCallBack {
        void onRequestErr();

        /**
         * 此方法在PlayLoadLayoutPlayerLibs的requestError()和ipError()中被调用
         */
        void calledInError();

        /**
         * 此方法在PlayLoadLayoutPlayerLibs的finish()中被调用
         */
        void calledInFinish();
    }

    public boolean isShowLoading() {
        return loading.getVisibility() == View.VISIBLE;
    }
}
